package com.web_bloks.content.client;

import java.util.HashMap;

import com.web_bloks.document.client.Namespace;
import com.web_bloks.document.client.Title;
import com.web_bloks.system.client.Logger;

/**
 * 
 * @author Jean-Lou Dupont
 *
 */
abstract public class TitleAccessList {

	static Title WILDTITLE = null; 
	
	HashMap<Title, Condition[]> map = null;
	
	/*****************************************
	 * CONSTRUCTOR
	 *****************************************/
	protected TitleAccessList() {
		map = new HashMap<Title, Condition[]>();
		if (null == WILDTITLE)
			WILDTITLE = Title.newWild(); 
	}
	
	/*****************************************
	 * CONFIGURATION
	 *****************************************/
	protected void addMapping( Title title, Condition[] conditions) {
		map.put( title, conditions );
	}
	
	/*****************************************
	 * API
	 *****************************************/
	
	/**
	 * @return NULL if no match found
	 */
	public Condition[] findMatch(Title title) {

		if (null == title)
			throw new RuntimeException(this.getClass()+".findMatch: title can't be null");
		
		//1st
		if ( map.containsKey(title) ) {
			Logger.logInfo(this.getClass()+".findMatch: NORMAL");
			return map.get(title);
		}
			
		Namespace ns = title.getNamespace();
		Title wt = Title.newWild(ns);
		
		//2nd
		if ( map.containsKey(wt) ) {
			return map.get(wt);			
		}
		
		//3rd
		if ( map.containsKey(WILDTITLE) ) {
			Logger.logInfo(this.getClass()+".findMatch: DEFAULT");			
			return map.get(WILDTITLE);
		}
		
		Logger.logInfo(this.getClass()+".findMatch: NOT FOUND");
				
		return null;
	}
	
	/*****************************************
	 * HELPERS
	 *****************************************/
	public static Title getWildTitle() {
		return WILDTITLE;
	}
	
}//END